const pomelo = window.pomelo;
const testUid = "test2"
//const testUid = "oCnyx1CTpOvYISzDEpnNZPFJaT9c"
let connHost;   //connector地址
let connPort;   //connector端口
let isConnectorEntry=false;    //是否连接Connector

//Gate获取Connector端口地址
//首先需要在初始化数据库数据
function gateConnector(elem) {
  let route = "gate.gateHandler.entry";
  let gateHost = "127.0.0.1";
  //let gateHost = "39.98.59.196";
  let gatePort = 3110;
  return new Promise( resolve => {
    pomelo.init({ host: gateHost, port: gatePort, log: true }, function(err) {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("Gate获取Connector端口地址");
        showLog(JSON.stringify(data));
        if(!data || data.code != 200) return;
        elem.setAttribute("disabled","");
        connHost = data.host;
        connPort = data.port;
        resolve();
      });
    });
  });
}

//连接Connector
async function connectorEntry(elem) {
  let route = "connector.entryHandler.entry";
  if(!connHost || !connPort) 
    await gateConnector(document.getElementById("gateBtn"));
  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.init({ host: connHost, port: connPort, log: true }, function() {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("初始化connector成功");
        elem.setAttribute("disabled","");
        isConnectorEntry = true;
        resolve();
      });
    });
  });
}

//注册
async function register(elem) {
  let route = "login.loginHandler.register";
  if(!isConnectorEntry) 
    await connectorEntry(document.getElementById("connBtn"));
  return new Promise( resolve => {
    pomelo.request(route, { 
      nickname: "昵称", 
      union_id: "union-id-abc",
      avatar: "https://wx.qlogo.cn/mmopen/vi_32/pBIaptYeKRuWSedSzxicvVhB8rJOEUQeLXLhUpWGFZkTvco5icLtg7gWMibbHGdV9QfhvArgW3cjA6TlQk4iadpTng/132", 
      gender: 1, 
      code: "5223",
      city: "太原", 
      province: "山西", 
      phone: "15234532582", 
    } , data => {
      showLog(JSON.stringify(data));
    });
  });
}

//用户登陆
async function login(elem) {
  let route = "login.loginHandler.login";
  if(!isConnectorEntry) 
    await connectorEntry(document.getElementById("connBtn"));
  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      if(data.code !== 200) return;
      showLog("用户登陆成功");
      console.log(data);
      showLog("离线金币"+JSON.stringify({
        logOutCoins: data.userInfo.logOutCoins,    //离线金币
        logOutInterval: data.userInfo.logOutInterval,    //离线时长
      }));
      elem.setAttribute("disabled","");
      elem.setAttribute("value", testUid+"登陆中");
      resolve();
    });
  });
}

//发送验证码
async function sendPhone(elem) {
  let route = "login.loginHandler.sendCode";
  if(!isConnectorEntry) 
    await connectorEntry(document.getElementById("connBtn"));
  return new Promise( resolve => {
    pomelo.request(route, { phone: "15234532582" } , data => {
      showLog(JSON.stringify(data));
    });
  });
}

//修改联系方式
async function updateContactInfo(elem){
  let route = "login.loginHandler.updateContactInfo";
  return new Promise( resolve => {
    pomelo.request(route, { contactInfo: "15234532582123" } , data => {
      showLog(JSON.stringify(data));
    });
  });
}

//获取签名
async function getContactInfo(elem){
  let route = "login.loginHandler.getContactInfo";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog(JSON.stringify(data));
    });
  });
}

//显示日志
function showLog(log){
  let logPanel = document.getElementById("showLog");
  let textobj=document.createTextNode(log+"\n");
  logPanel.prepend(textobj);
}
